Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tests): adds basic testing support #8

Merged
merged 8 commits into from
Nov 1, 2019
Merged

Conversation

alvincrespo
Copy link

@alvincrespo alvincrespo commented Oct 30, 2019

Changes

  • Adds ts-jest as a dependency
  • Updates test script to run jest
  • Moves template dir logic to it's own utility
  • Tests out the template dir logic with sample test

Fixes #1

@alvincrespo alvincrespo self-assigned this Oct 30, 2019
__mocks__/fs.ts Outdated
@@ -0,0 +1,5 @@
const fs = jest.genMockFromModule("fs") as any;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implement correct type

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have @types/node which should include fs (https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/node/fs.d.ts#L1595). what was the error you were getting here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried:

import * as fsModule from "fs";

const fs = jest.genMockFromModule<fsModule>("fs");

fs.existsSync = () => true;
 
module.exports = fs;

But that gives me:

image

Doing some searching and found, that that is expected. Not really sure how to type this? 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jest.config.js Outdated
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any other config we can think of for now?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing at the moment. Thoughts on adding this to the package.json instead of a separate file?

@alvincrespo alvincrespo marked this pull request as ready for review October 30, 2019 18:18
Copy link
Member

@cball cball left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few suggestions. Looking 🎸 though!

__mocks__/fs.ts Outdated
@@ -0,0 +1,5 @@
const fs = jest.genMockFromModule("fs") as any;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have @types/node which should include fs (https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/node/fs.d.ts#L1595). what was the error you were getting here?

@@ -1 +1 @@
export {run} from '@oclif/command'
export { run } from "@oclif/command";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we have prettier set for this? If not we should (typically spaces before curlies and single quotes

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, this is a global prettier config I'm using - we don't have one on the project yet.

const templateLocation = getTemplateLocation();

expect(templateLocation).toEqual(`HYGEN_TMPLS=/custom/path/to/_templates`);
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎸

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great test 👏

src/utils/getTemplateLocation.ts Outdated Show resolved Hide resolved
@cball cball changed the title feat: adds basic testing support feat (tests): adds basic testing support Oct 30, 2019
@cball cball changed the title feat (tests): adds basic testing support feat(tests): adds basic testing support Oct 30, 2019
Copy link
Contributor

@jsjoeio jsjoeio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, I think we're close to approving, just a few questions


fs.existsSync = () => true;

module.exports = fs;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are your thoughts on mocking the function instead of mocking the whole module?

const mock = jest.spyOn(fs, 'readFileSync');  // spy on fs.readFileSync()
    mock.mockImplementation(() => JSON.stringify({ name: 'myname' }));

Would that a better idea? And would it solve the type issue we're currently trying to solve?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately not.

Screen Shot 2019-10-31 at 7 29 10 AM

Since the instance of the import in the test is different in the actual app code, the mock doesn't apply to the right one AFAIK.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, gotcha. Well, I don't think we should hold this back for that reason.

Ping me when you think this is ready to go and I'll approve!

jest.config.js Outdated
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing at the moment. Thoughts on adding this to the package.json instead of a separate file?

const templateLocation = getTemplateLocation();

expect(templateLocation).toEqual(`HYGEN_TMPLS=/custom/path/to/_templates`);
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great test 👏

const hasTemplates = fs.existsSync(theirTemplatePath);

export const getTemplateLocation = (): string =>
hasTemplates ? theirTemplatePath : DEFAULT_TEMPLATE_PATH;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love how clean this is! 👍 💯 Nice work!

@jsjoeio
Copy link
Contributor

jsjoeio commented Nov 1, 2019

@alvincrespo we updated the CircleCI config so that I can leverage this in my tests

@jsjoeio
Copy link
Contributor

jsjoeio commented Nov 1, 2019

@all-contributors please add @alvincrespo for code, doc, test and review

@allcontributors
Copy link
Contributor

@jsjoeio

I've put up a pull request to add @alvincrespo! 🎉

@jsjoeio jsjoeio merged commit 5719cfd into release Nov 1, 2019
@jsjoeio jsjoeio deleted the ac/add-testing-support branch November 1, 2019 17:40
@jsjoeio
Copy link
Contributor

jsjoeio commented Nov 1, 2019

@all-contributors please add @cball for infra, ideas, and review

@allcontributors
Copy link
Contributor

@jsjoeio

I've put up a pull request to add @cball! 🎉

jsjoeio pushed a commit that referenced this pull request Nov 11, 2019
* BREAKING CHANGE: modify how generate command works

* docs(contrib): add ci and update generate command docs (#6)

* feat: update README and add CONTRIBUTING

* fix: update generate command args

* feat: update docs with new generate command info

* docs: add jsjoeio as a contributor (#13)

* docs: update README.md

* docs: create .all-contributorsrc

* chore: add @semantic-release/npm & .../git plugins (#14)

* feat: add semantic-release/git

* feat: add npm plugin as well

* chore(templates): add issue template (#15)

* Update issue templates

Add a simple issue report template

* Create pull_request_template.md

add PR template

* docs: add cmejet as a contributor (#16)

* docs: update README.md

* docs: update .all-contributorsrc

* change name to bug instead of issue

It makes more sense to call this bug because it's a bug report issue template

* Create feature_request.md

add feature request template

* Update and rename issue-report.md to bug_ report.md

* chore: clean up template

Remove stuff specific to Gatsby

* feat(command): add init script (#11)

* feat: add init command

* feat: add node types

* feat: add write to package.json in init

* refactor: write to package.json

* feat: add install eb-scripts line

* refactor: change name of vars

* chore: update oclif.manifest and README

* Update src/commands/init.ts

Co-Authored-By: Chris Ball <chris@echobind.com>

* fix: modify order of updatedScripts

* docs: add alvincrespo as a contributor (#22)

* docs: update README.md

* docs: update .all-contributorsrc

* feat(tests): adds basic testing support (#8)

* first commit

* moar progress

* passing tests

* reorg deps

* cast as any properly

* updating per review

* move jest config into package.json

* feat: update circleci config

* docs: add cball as a contributor (#23)

* docs: update README.md

* docs: update .all-contributorsrc

* chore(project): add prettier, eslint and husky (#26)

* feat: add prettier and husky

* chore: add eslint and deps

* chore: add eslint plugins prettier and eb

* feat(testing): add integration test for generate command (#24)

* fix: add back in semantic release plugins

* fix: add jest config and install fs-extra

* feat: add makeTempDir and removeTempDir utils

* wip: add initial generate test

* feat: add util fn to check if dir exists

* refactor: change names temp dir fns

* refactor: add index to simplify exports

* wip: add test with oclif

* feat: add checkFileExists util

* chore: update deps

* chore: add checkFileExists to index

* wip: almost there

* wip: generate test

* feat: add test for no flags

* wip: add oclif test

* feat: write snapshot test for generate error

* chore: clean up deps

* wip: add test to check custom templates

* feat: add test to check using custom templates

* refactor: use copyFile instead of writeFile

* feat: update snapshot and get test passing default

* refactor: create tests/helpers and move template

* chore: update order of types in package.json

* refactor: remove proxy fse fns

* refactor: clean up getTemplateLocation

* docs: add jeffreyzhen as a contributor (#35)

* docs: update README.md

* docs: update .all-contributorsrc

* refactor: update CONTRIBUTING PR process

* refactor: update PR template to add check for base branch

* chore(release): add commit-analyzer and release-notes-generator (#30)

* feat: add commit analyzer and rel not generator semantic release

* fix: only release if semantic-release passes

* refactor: use npm ci in circleci

* refactor: use yarn in circleci config

* fix: typo in release section

* fix: switch back to npm in config

* fix: change back to npm install

* feat(generate, init): add support for react-typescript (#36)

* fix: add back in semantic release plugins

* fix: add jest config and install fs-extra

* feat: add makeTempDir and removeTempDir utils

* feat: add util fn to check if dir exists

* refactor: change names temp dir fns

* wip: add test with oclif

* feat: add checkFileExists util

* wip: almost there

* wip: generate test

* feat: add test for no flags

* wip: add oclif test

* feat: write snapshot test for generate error

* chore: clean up deps

* wip: add test to check custom templates

* feat: add test to check using custom templates

* refactor: use copyFile instead of writeFile

* refactor: create tests/helpers and move template

* chore: update order of types in package.json

* refactor: remove proxy fse fns

* feat: add initial init test

* wip: get tests setup for init

* feat: finish yarn lock

* feat: add test for init without flag

* feat: add validProjectTypes to init, and test

* fix: run tests sequentially with --runInBand

* refactor: clean up unused tests init

* fix: update README formatting

* fix: update getTemplateLocation test

* refactor: fix getTemplateLocation test

* refactor: move afterEach and afterAll into describe block

* fix: switch to yarn

* fix: update test description

* docs: add comment about dup import of fn

* fix: update test description in init.test

* refactor: use flag or args for init, remove -p

* chore: update README

* wip: update init and test to make project required

* fix: tell jest to ignore lib

* fix: update init tests and snap

* feat: add path flag to generate and update temps

* fix: update generate to throw on no args

* fix: remove extra space in templates

* fix: update test description

* fix: update log statement in generate

* fix: remove other extra space

* fix: update old test

* fix: update helper template for test

* feat: add test for location flag

* fix: unskip tests

* refactor: remove side effect in last test

* feat: add -p to script in init

* refactor: unskip and use all tests

* fix: update test after rebase

* fix: update snapshot after rebase not updating it

* feat: add react-typescript-component templates

* refactor: improve typings in generate command

* refactor: remove old template flag from generate

* wip: add test for react-typescript-component

* feat: add react-typescript-component as new temp type

* fix: typos in react-ts-component template

* fix: remove old copyFile fn

* feat: add react-typescript to init projects

* feat: update snapshots and add test

* refactor: require to readFile to prevent caching

* fix: remove console.logs

* docs: update with new command info

* fix: update item in checklist for PR

* fix: update circle ci config

* fix: switch back to yarn

* feat(generate, init): allow user to override path for templates (#33)

* fix: add back in semantic release plugins

* fix: add jest config and install fs-extra

* feat: add makeTempDir and removeTempDir utils

* feat: add util fn to check if dir exists

* refactor: change names temp dir fns

* wip: add test with oclif

* feat: add checkFileExists util

* wip: almost there

* wip: generate test

* feat: add test for no flags

* wip: add oclif test

* feat: write snapshot test for generate error

* chore: clean up deps

* wip: add test to check custom templates

* feat: add test to check using custom templates

* refactor: use copyFile instead of writeFile

* refactor: create tests/helpers and move template

* chore: update order of types in package.json

* refactor: remove proxy fse fns

* fix: run tests sequentially with --runInBand

* fix: update getTemplateLocation test

* refactor: fix getTemplateLocation test

* chore: update README

* fix: update init tests and snap

* fix: update generate to throw on no args

* fix: update test description

* fix: update old test

* refactor: remove side effect in last test

* feat: add -p to script in init

* refactor: unskip and use all tests

* fix: update test after rebase

* fix: update snapshot after rebase not updating it

* fix: errors after rebase

* fix: update snapshot

* feat(generate, init): add support for react-native-typescript (#37)

* feat: add component template

* refactor: change name to react-native-typescript

* refactor: change name to typescript component

* feat: add react-typescript-component to generate

* feat: add test for react-native-typescript-component

* feat: add react-native-typescript to init

* feat: add react-native-typescript screen template

* feat: add react-native-typescript-screen to generate

* wip: generate screen test

* wip: add note about screen template test

* feat: add e2e template

* feat: add e2e to init command

* feat: add e2e to generate

* fix: update quotes to double in e2e template

* feat: add e2e to react-native-ts and add tset

* feat: add e2e template test

* fix: update snapshot after changes

* feat: add util-typescript-template

* feat: add util-typescript to generate and init

* fix: add test and default path for utils temp

* fix: update generate test

* fix: update generate snapshot after new util template

* fix: update docs

* fix: clean up from comments

* fix: comment out skipped test

* fix: change component name in test

* fix: update version to match npm version

* fix: update oclif manifest to match master

* fix: update PR template

* fix: update bug report

* [ci skip]: fix version issue

* docs(contrib): add ci and update generate command docs (#6)

* feat: update README and add CONTRIBUTING

* fix: update generate command args

* feat: update docs with new generate command info

* chore(templates): add issue template (#15)

* Update issue templates

Add a simple issue report template

* Create pull_request_template.md

add PR template

* Update and rename issue-report.md to bug_ report.md

* feat(command): add init script (#11)

* feat: add init command

* feat: add node types

* feat: add write to package.json in init

* refactor: write to package.json

* feat: add install eb-scripts line

* refactor: change name of vars

* chore: update oclif.manifest and README

* Update src/commands/init.ts

Co-Authored-By: Chris Ball <chris@echobind.com>

* fix: modify order of updatedScripts

* feat(generate, init): add support for react-typescript (#36)

* fix: add back in semantic release plugins

* fix: add jest config and install fs-extra

* feat: add makeTempDir and removeTempDir utils

* feat: add util fn to check if dir exists

* refactor: change names temp dir fns

* wip: add test with oclif

* feat: add checkFileExists util

* wip: almost there

* wip: generate test

* feat: add test for no flags

* wip: add oclif test

* feat: write snapshot test for generate error

* chore: clean up deps

* wip: add test to check custom templates

* feat: add test to check using custom templates

* refactor: use copyFile instead of writeFile

* refactor: create tests/helpers and move template

* chore: update order of types in package.json

* refactor: remove proxy fse fns

* feat: add initial init test

* wip: get tests setup for init

* feat: finish yarn lock

* feat: add test for init without flag

* feat: add validProjectTypes to init, and test

* fix: run tests sequentially with --runInBand

* refactor: clean up unused tests init

* fix: update README formatting

* fix: update getTemplateLocation test

* refactor: fix getTemplateLocation test

* refactor: move afterEach and afterAll into describe block

* fix: switch to yarn

* fix: update test description

* docs: add comment about dup import of fn

* fix: update test description in init.test

* refactor: use flag or args for init, remove -p

* chore: update README

* wip: update init and test to make project required

* fix: tell jest to ignore lib

* fix: update init tests and snap

* feat: add path flag to generate and update temps

* fix: update generate to throw on no args

* fix: remove extra space in templates

* fix: update test description

* fix: update log statement in generate

* fix: remove other extra space

* fix: update old test

* fix: update helper template for test

* feat: add test for location flag

* fix: unskip tests

* refactor: remove side effect in last test

* feat: add -p to script in init

* refactor: unskip and use all tests

* fix: update test after rebase

* fix: update snapshot after rebase not updating it

* feat: add react-typescript-component templates

* refactor: improve typings in generate command

* refactor: remove old template flag from generate

* wip: add test for react-typescript-component

* feat: add react-typescript-component as new temp type

* fix: typos in react-ts-component template

* fix: remove old copyFile fn

* feat: add react-typescript to init projects

* feat: update snapshots and add test

* refactor: require to readFile to prevent caching

* fix: remove console.logs

* docs: update with new command info

* fix: update item in checklist for PR

* fix: update circle ci config

* fix: switch back to yarn

* feat(generate, init): allow user to override path for templates (#33)

* fix: add back in semantic release plugins

* fix: add jest config and install fs-extra

* feat: add makeTempDir and removeTempDir utils

* feat: add util fn to check if dir exists

* refactor: change names temp dir fns

* wip: add test with oclif

* feat: add checkFileExists util

* wip: almost there

* wip: generate test

* feat: add test for no flags

* wip: add oclif test

* feat: write snapshot test for generate error

* chore: clean up deps

* wip: add test to check custom templates

* feat: add test to check using custom templates

* refactor: use copyFile instead of writeFile

* refactor: create tests/helpers and move template

* chore: update order of types in package.json

* refactor: remove proxy fse fns

* fix: run tests sequentially with --runInBand

* fix: update getTemplateLocation test

* refactor: fix getTemplateLocation test

* chore: update README

* fix: update init tests and snap

* fix: update generate to throw on no args

* fix: update test description

* fix: update old test

* refactor: remove side effect in last test

* feat: add -p to script in init

* refactor: unskip and use all tests

* fix: update test after rebase

* fix: update snapshot after rebase not updating it

* fix: errors after rebase

* fix: update snapshot

* feat(generate, init): add support for react-native-typescript (#37)

* feat: add component template

* refactor: change name to react-native-typescript

* refactor: change name to typescript component

* feat: add react-typescript-component to generate

* feat: add test for react-native-typescript-component

* feat: add react-native-typescript to init

* feat: add react-native-typescript screen template

* feat: add react-native-typescript-screen to generate

* wip: generate screen test

* wip: add note about screen template test

* feat: add e2e template

* feat: add e2e to init command

* feat: add e2e to generate

* fix: update quotes to double in e2e template

* feat: add e2e to react-native-ts and add tset

* feat: add e2e template test

* fix: update snapshot after changes

* feat: add util-typescript-template

* feat: add util-typescript to generate and init

* fix: add test and default path for utils temp

* fix: update generate test

* fix: update generate snapshot after new util template

* fix: update docs

* fix: clean up from comments

* fix: comment out skipped test

* fix: change component name in test

* fix: remove flag from yarn test

* fix: update snapshots

* fix: updte bin run

* fix: update oclif and init snapshot

* reactor: export all utils

* refactor: move test higher

* fix: update snapshots and add runInBand flag to yarn test

* fix: update PR template to remove check for next branch
jsjoeio pushed a commit that referenced this pull request Nov 11, 2019
# [2.0.0](v1.0.0...v2.0.0) (2019-11-11)

### Features

* **generate, init:** create new release ([#39](#39)) ([c4d4f54](c4d4f54)), closes [#6](#6) [#13](#13) [#14](#14) [#15](#15) [#16](#16) [#11](#11) [#22](#22) [#8](#8) [#23](#23) [#26](#26) [#24](#24) [#35](#35) [#30](#30) [#36](#36) [#33](#33) [#37](#37) [#6](#6) [#15](#15) [#11](#11) [#36](#36) [#33](#33) [#37](#37)

### BREAKING CHANGES

* **generate, init:** modify how generate command works
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Write test for generate command
3 participants